package com.nrg.richie.server;

import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Locale;
import java.util.Properties;
import java.util.logging.Logger;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.fileupload.FileItemIterator;
import org.apache.commons.fileupload.FileItemStream;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

import com.nrg.richie.server.utils.StringUtils;
import com.nrg.richie.server.utils.XMLUtils;
import com.nrg.richie.shared.NodeData;

@SuppressWarnings("serial")
public class UploadServlet extends HttpServlet {

	public static final String XML_DOCUMENT = "xmlDocument";
	public static final String NODE_DATA_LIST = "NodeDataList";
	public static final String XML_DOCUMENT_FILTERED = "xmlDocumentFiltered";
	Logger logger = Logger.getLogger(UploadServlet.class.getName());
	
    public void doPost(HttpServletRequest request, HttpServletResponse response)  throws ServletException {

        ServletFileUpload upload = new ServletFileUpload();

        Document document = null;
        
        try{
            FileItemIterator iter = upload.getItemIterator(request);

            while (iter.hasNext()) {
                FileItemStream item = iter.next();

                if (item.getContentType().toLowerCase().indexOf("zip") < 0) {
                	continue;
                }
                
                InputStream stream = item.openStream();
                
                // Open the ZIP file
                 ZipInputStream in = new ZipInputStream(stream);

                // Get the first entry
                @SuppressWarnings("unused")
				ZipEntry entry = in.getNextEntry();

                document = XMLUtils.getXml(in);
                
                // Close the streams
                in.close();

                //only one file gets processed
                break;
            }

        }
        catch(Exception e){
        	logger.severe(StringUtils.getStacktraceAsString(e));            
        }
        
        
        if (document != null) {

        	HttpSession session = request.getSession();

        	session.setAttribute(XML_DOCUMENT, XMLUtils.getXmlString(document));
        	session.setAttribute(NODE_DATA_LIST, XMLUtils.processXml(document, request.getLocale()));

        } else {
        	PrintWriter out;
			try {
				out = response.getWriter();
	    	    response.setContentType("text/plain");
	    	    out.print("<h1>Hib�s felt�lt�tt file.</h1>");
	    	    out.close();
			} catch (IOException e) {
				logger.severe(StringUtils.getStacktraceAsString(e));
			}

        }
        
    }
    }